/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jgc.gredos_3d;

import android.opengl.GLSurfaceView;

import com.jgc.gredos_3d.R;
import com.jgc.gredos_3d.SimpleGLRenderer;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
// import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.MotionEvent;

// The main entry point for the actual landscape rendering test.
// This class pulls options from the preferences set in the MainMenu
// activity and builds the landscape accordingly.
public class Gredos_3D extends Activity {
	private GLSurfaceView mGLSurfaceView;
	private SimpleGLRenderer mSimpleRenderer;
	private Game mGame;
	private Thread mGameThread;
	private float mLastScreenX;
	private float mLastScreenY;
	private Bitmap heightmap;
	private Bitmap lightmap;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mGLSurfaceView = new GLSurfaceView(this);
		mGLSurfaceView.setEGLConfigChooser(true);

		mSimpleRenderer = new SimpleGLRenderer(this);

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		final boolean runGame = prefs.getBoolean("runsim", true);
		final boolean bigWorld = prefs.getBoolean("bigworld", false);
		final boolean skybox = prefs.getBoolean("skybox", true);
		final boolean texture = prefs.getBoolean("texture", true);
		final boolean vertexColors = prefs.getBoolean("colors", false);
		final boolean useTextureLods = prefs.getBoolean("lodTexture", true);
		final boolean textureMips = prefs.getBoolean("textureMips", true);
		final boolean useColorTextureLods = prefs.getBoolean(
				"lodTextureColored", false);
		final int maxTextureSize = Integer.parseInt(prefs.getString(
				"maxTextureSize", "2048"));
		final String textureFilter = prefs.getString("textureFiltering",
				"trilinear");
		final boolean useLods = prefs.getBoolean("lod", true);
		final int complexity = Integer.parseInt(prefs.getString("complexity",
				"128"));
		final boolean useFixedPoint = prefs.getBoolean("fixed", true);
		final boolean useVbos = prefs.getBoolean("vbo", true);

		// LOADING Bitmap Images:

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = false;

		// BitmapDrawable heightMapDrawable =
		// (BitmapDrawable)getResources().getDrawable(R.drawable.gredos_15km_h);

		heightmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.heightmap, options);
		// heightMapDrawable.getBitmap();

		 lightmap = null;
		if (vertexColors != false) {
			// BitmapDrawable lightMapDrawable =
			// (BitmapDrawable)getResources().getDrawable(R.drawable.lightmap);
			lightmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.lightmap, options);
			// lightMapDrawable.getBitmap();
		}

		LandTileMap tileMap = null;
		if (bigWorld) {

			// This set the number of X Tiles and Y Tiles:
			final int tilesX = 4;
			final int tilesY = 4;
			tileMap = new LandTileMap(tilesX, tilesY, heightmap, lightmap,
					vertexColors, texture, useLods, complexity, useFixedPoint);

		} else {
			tileMap = new LandTileMap(1, 1, heightmap, lightmap, vertexColors,
					texture, useLods, complexity, useFixedPoint);
		}

		if (skybox) {
			// BitmapDrawable skyboxDrawable =
			// (BitmapDrawable)getResources().getDrawable(R.drawable.skybox);
			Bitmap skyboxBitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.skybox, options);
			// skyboxDrawable.getBitmap();
			tileMap.setupSkybox(skyboxBitmap, useFixedPoint);
		}

		ProfileRecorder.sSingleton.resetAll();
		mSimpleRenderer.setUseHardwareBuffers(useVbos);
		if (texture) {
			mSimpleRenderer.setTiles(tileMap, R.drawable.terrain,
					R.drawable.skybox_texture);
			mSimpleRenderer.setUseTextureLods(useTextureLods, textureMips);
			mSimpleRenderer.setColorTextureLods(useColorTextureLods);
			mSimpleRenderer.setMaxTextureSize(maxTextureSize);
			if (textureFilter == "nearest") {
				mSimpleRenderer
						.setTextureFilter(SimpleGLRenderer.FILTER_NEAREST_NEIGHBOR);
			} else if (textureFilter == "trilinear") {
				mSimpleRenderer
						.setTextureFilter(SimpleGLRenderer.FILTER_TRILINEAR);
			} else {
				mSimpleRenderer
						.setTextureFilter(SimpleGLRenderer.FILTER_BILINEAR);
			}

		} else {
			mSimpleRenderer.setTiles(tileMap, 0, 0);

		}
		mGLSurfaceView.setRenderer(mSimpleRenderer);
		setContentView(mGLSurfaceView);

		if (runGame) {
			mGame = new Game(mSimpleRenderer, tileMap);
			mGameThread = new Thread(mGame);
			mGameThread.start();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mGLSurfaceView.onPause();
		if (mGame != null) {
			mGame.pause();
			
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mGLSurfaceView.onResume();
		if (mGame != null) {
			try {
				mGame.resume();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		if (mGame != null) {
			mGame.rotate(event.getRawX(), -event.getRawY());
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGame != null) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				mGame.move(1.0f);
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				float xDelta = event.getX() - mLastScreenX;
				float yDelta = event.getY() - mLastScreenY;
				// mGame.move(1.0f);
				// Scale the values we got down to make control usable.
				// A real game would probably figure out scale factors based
				// on the size of the screen rather than hard-coded constants
				// like this.
				mGame.rotate(xDelta * 0.01f, -yDelta * 0.005f);
			}
		}
		mLastScreenX = event.getX();
		mLastScreenY = event.getY();
		try {
			Thread.sleep(16);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (mGame != null) {
			final float speed = 1.0f;
			switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_DOWN:
				mGame.rotate(0.0f, -speed);

				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				mGame.rotate(-speed, 0.0f);

				return true;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				mGame.rotate(speed, 0.0f);

				return true;
			case KeyEvent.KEYCODE_DPAD_UP:
				mGame.rotate(0.0f, speed);

				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}